/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.common.sdk.net.connect.http;

import java.io.Serializable;
import java.util.Collections;
import java.util.Map;

import org.apache.http.HttpStatus;

/**
 * Data and headers returned from {@link Network#performRequest(Request)}.
 */
public class NetworkResponseEx implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * Creates a new network response.
     * 
     * @param statusCode the HTTP status code
     * @param data Response body
     * @param headers Headers returned with this response, or null for none
     * @param notModified True if the server returned a 304 and the data was
     *            already in cache
     */
    public NetworkResponseEx(int statusCode, byte[] data, Map<String, String> headers, boolean notModified) {
        this.statusCode = statusCode;
        this.data = data;
        this.headers = headers;
        this.notModified = notModified;
    }

    public NetworkResponseEx(byte[] data) {
        this(HttpStatus.SC_OK, data, Collections.<String, String> emptyMap(), false);
    }

    public NetworkResponseEx(byte[] data, Map<String, String> headers) {
        this(HttpStatus.SC_OK, data, headers, false);
    }

    /** The HTTP status code. */
    public int statusCode;

    /** Raw data from this response. */
    public byte[] data;

    /** Response headers. */
    public Map<String, String> headers;

    /** True if the server returned a 304 (Not Modified). */
    public boolean notModified;

    public String jsonData;

    public Object parsedData;

    /** 超时时间 */
    protected int validInterval;

    /** 保存时的时间 */
    protected long saveTime;

    /** True if the entry is expired. */
    public boolean isExpired() {
        /**
         * <!--boyang116245@sohu-inc.com 20140411 最终时间大于等于当前时间，说明数据有效，没有过期 -->
         */
        if (Math.abs(System.currentTimeMillis() - saveTime) < validInterval * 1000) {
            return false;
        }
        /**
         * <!--boyang116245@sohu-inc.com 20140411 最终时间大于等于当前时间，说明数据有效，没有过期 END
         * -->
         */
        return true;
    }

    public void setValidInterval(int validInterval) {
        this.validInterval = validInterval;
    }

    public void setSaveTime(long saveTime) {
        this.saveTime = saveTime;
    }

    public long getSaveTime() {
        return saveTime;
    }

    public int getValidInterval() {
        return validInterval;
    }

    public void setParsedData(Object parsedData) {
        this.parsedData = parsedData;
    }

    public Object getParsedData() {
        return parsedData;
    }

    public String getJsonData() {
        return jsonData;
    }

    public void setJsonData(String jsonData) {
        this.jsonData = jsonData;
    }
}